package com.sanshengshui.iot.web;


import com.sanshengshui.iot.common.message.*;
import com.sanshengshui.iot.common.session.GrozaSessionStoreService;
import com.sanshengshui.iot.common.session.SessionStore;
import com.sanshengshui.iot.common.subscribe.GrozaSubscribeStoreService;
import com.sanshengshui.iot.common.subscribe.SubscribeStore;
import com.sanshengshui.iot.util.KafkaMockUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.DataType;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/groza/v1")
@Slf4j
public class
TestController {



    @Autowired
    GrozaSessionStoreService sessionStoreService;

    @Autowired
    GrozaSubscribeStoreService  subscribeStoreService;


    @Autowired
    GrozaDupPublishMessageStoreService dupPublishMessageStoreService;
    @Autowired
    GrozaRetainMessageStoreService retainMessageStoreService;


    @Autowired
    GrozaDupPubRelMessageStoreService dupPubRelMessageStoreService;

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping(value = "getAllMsg")
    public List<String> getAllMsg(){
        return KafkaMockUtil.getAllMsg();
    }


    @RequestMapping(value = "getSession")
    public SessionStore getSession(String clientId){

        return sessionStoreService.get(clientId);
    }

    @RequestMapping(value = "seachSubscribe")
    public List<SubscribeStore> seachSubscribe(String topic){

        // 发布消息时使用
        // Publish.sendPublishMessage
        return subscribeStoreService.search(topic);
    }

    @RequestMapping(value = "getDupPublishMessageStore")
    public List<DupPublishMessageStore> getDupPublishMessageStore(String clientId){

        return dupPublishMessageStoreService.get(clientId);
    }

    @RequestMapping(value = "getDupPubRelMessageStore")
    public List<DupPubRelMessageStore> getDupPubRelMessageStore(String clientId){

        return dupPubRelMessageStoreService.get(clientId);
    }


    @RequestMapping(value = "getRetainMessage")
    public RetainMessageStore getRetainMessage(String topic){

        return retainMessageStoreService.get(topic);
    }

    @RequestMapping(value = "searchRetainMessage")
    public List<RetainMessageStore> searchRetainMessage(String topicFilter){

        return retainMessageStoreService.search(topicFilter);
    }


    @RequestMapping(value = "redisKeys")
    public Set<String> redisKeys(String pattens){
       if(pattens==null){
           pattens = "";
       }
       if(StringUtils.isEmpty(pattens)){
           pattens = "groza:*";
       }

        return redisTemplate.keys(pattens);
    }

    @RequestMapping(value = "redisDelete")
    public Object redisDelete(String key){
        Map map = new HashMap();
        Boolean result = redisTemplate.delete(key);
        map.put("result",result);
        map.put("key",key);


        return map;
    }

    @RequestMapping(value = "redisType")
    public Object redisType(String key){
        return redisTemplate.type(key);
    }

    @RequestMapping(value = "redisGet")
    public Object redisGet(String key){
        Map map = new HashMap();
        DataType type = redisTemplate.type(key);
        map.put("key",key);
        map.put("type",type+"");


        if(DataType.STRING.equals(type)){
            Object value = redisTemplate.opsForValue().get(key);
            map.put("value",value);
            return map;
        }

        if(DataType.HASH.equals(type)){
            Map value = redisTemplate.opsForHash().entries(key);
            map.put("value",value);
            return map;
        }

        if(DataType.SET.equals(type)){
            // JSON: Unrecognized token 'topic002': was expecting 'null', 'true', 'false' or NaN
            // Set value = redisTemplate.opsForSet().members(key);

            Set value = stringRedisTemplate.opsForSet().members(key);
            map.put("value",value);
            return map;
        }

        if(DataType.LIST.equals(type)){
            List value = redisTemplate.opsForList().range(key,0,-1);
            map.put("value",value);
            return map;
        }

        if(DataType.ZSET.equals(type)){
            Set value = redisTemplate.opsForZSet().range(key,0,-1);
            map.put("value",value);
            return map;
        }

        if(DataType.NONE.equals(type)){
            Object value = redisTemplate.opsForValue().get(key);
            map.put("value",value);
        }


        map.put("value","type not match");

        return map;
    }




}
